package com.fr.chicken.service.Impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.fr.chicken.dao.MessageLogDao;
import com.fr.chicken.dao.PlayerInfoDao;
import com.fr.chicken.facade.base.BaseResponseDTO;
import com.fr.chicken.facade.info.FriendApplyListInfo;
import com.fr.chicken.facade.info.GiveLogInfo;
import com.fr.chicken.facade.info.MessageListInfo;
import com.fr.chicken.facade.info.PrivateMessageListInfo;
import com.fr.chicken.facade.request.GetPrivateMessageListRequsetDTO;
import com.fr.chicken.facade.request.SendSystemMessageRequsetDTO;
import com.fr.chicken.facade.request.SetIsReadRequsetDTO;
import com.fr.chicken.facade.response.GetFriendApplyListResponseDTO;
import com.fr.chicken.facade.response.GetGiveLogResponseDTO;
import com.fr.chicken.facade.response.GetMessageListResponseDTO;
import com.fr.chicken.facade.response.GetPrivateMessageListResponseDTO;
import com.fr.chicken.manager.message.MessageManager;
import com.fr.chicken.manager.playerinfo.PlayerInfoManager;
import com.fr.chicken.model.MessageLog;
import com.fr.chicken.model.PlayerInfo;
import com.fr.chicken.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service("messageService")
public class MessageServiceImpl implements MessageService {

    @Autowired
    private MessageLogDao messageLogDao;
    @Autowired
    private PlayerInfoDao playerInfoDao;
    @Autowired
    private MessageManager messageManager;
    @Autowired
    private PlayerInfoManager playerInfoManager;

    @Override
    public BaseResponseDTO sendSystemMessage(SendSystemMessageRequsetDTO requsetDTO) {
        BaseResponseDTO responseDTO=new BaseResponseDTO();
        MessageLog messageLog = new MessageLog();
        messageLog.setSenderId((long)0);
        messageLog.setRecipientId((long)0);
        messageLog.setIsRead((byte)2);
        messageLog.setContent(requsetDTO.getContent());
        messageLog.setSendTime(System.currentTimeMillis() / 1000);
        messageLog.setType((byte)6);
        int num = messageLogDao.insertSelective(messageLog);
        if (num<1){
            responseDTO.faild();
            responseDTO.setMsg("发送失败");
            return responseDTO;
        }
        responseDTO.success();
        responseDTO.setMsg("发送成功");
        return responseDTO;
    }

    @Override
    public GetGiveLogResponseDTO getGiveLog(Long playerId) {
        GetGiveLogResponseDTO responseDTO = new GetGiveLogResponseDTO();
        List<MessageLog> messageLogs = messageLogDao.selectBySenderIdAndType(playerId,(byte)3);
        List<MessageLog> messageLogs1 = messageLogDao.selectByRecipientIdAndType(playerId,(byte)3);
        List<GiveLogInfo> giveLogInfos = new ArrayList<>();
        for (MessageLog x:messageLogs){
            PlayerInfo playerInfo = playerInfoDao.selectByPrimaryKey(x.getRecipientId());
            GiveLogInfo giveLogInfo = new GiveLogInfo();
            giveLogInfo.setTime(x.getSendTime());
            giveLogInfo.setContent("你向"+playerInfo.getNickName()+"赠送"+x.getValue1()+"个鸡蛋");
            giveLogInfos.add(giveLogInfo);
        }
        for (MessageLog x1:messageLogs1){
            PlayerInfo playerInfo1 = playerInfoDao.selectByPrimaryKey(x1.getSenderId());
            GiveLogInfo giveLogInfo1 = new GiveLogInfo();
            giveLogInfo1.setTime(x1.getSendTime());
            giveLogInfo1.setContent(playerInfo1.getNickName()+"向你赠送"+x1.getValue1()+"个鸡蛋");
            giveLogInfos.add(giveLogInfo1);
        }
        responseDTO.setGiveLogInfos(giveLogInfos);
        responseDTO.success();
        responseDTO.setMsg("查询成功");
        return responseDTO;
    }

    @Override
    public GetPrivateMessageListResponseDTO getPrivateMessageList(GetPrivateMessageListRequsetDTO requsetDTO) {
        GetPrivateMessageListResponseDTO responseDTO = new GetPrivateMessageListResponseDTO();
        List<MessageLog> messageLogs = messageLogDao.selectBySenderIdAndRecipientIdAndType(requsetDTO.getPlayerId(),requsetDTO.getFriendId(),(byte)4);
        List<MessageLog> messageLogs1 = messageLogDao.selectBySenderIdAndRecipientIdAndType(requsetDTO.getFriendId(),requsetDTO.getPlayerId(),(byte)4);
        List<PrivateMessageListInfo> privateMessageListInfos = JSONArray.parseArray(JSON.toJSONString(messageLogs),PrivateMessageListInfo.class);
        List<PrivateMessageListInfo> privateMessageListInfos1 = JSONArray.parseArray(JSON.toJSONString(messageLogs1),PrivateMessageListInfo.class);
        responseDTO.setMyPrivateMessageListInfo(privateMessageListInfos);
        responseDTO.setFriendPrivateMessageListInfo(privateMessageListInfos1);
        responseDTO.success();
        responseDTO.setMsg("查询成功");
        return responseDTO;
    }

    @Override
    public GetMessageListResponseDTO getMessageList(Long playerId) {
        GetMessageListResponseDTO responseDTO = new GetMessageListResponseDTO();
        List<MessageLog> messageLogs = messageLogDao.selectByRecipientId(playerId);
        List<MessageListInfo> messageListInfos = new ArrayList<>();
        for (MessageLog x:messageLogs){
             if (x.getType()==4||x.getType()==2){
                    continue;
             }
            MessageListInfo messageListInfo = new MessageListInfo();
            messageListInfo.setId(x.getId());
            messageListInfo.setSendTime(x.getSendTime());
            messageListInfo.setContent(x.getContent());
            messageListInfo.setType(x.getType());
            messageListInfo.setIsRead(x.getIsRead());
            messageListInfo.setSenderId(x.getSenderId());
            messageListInfos.add(messageListInfo);
        }
        if (playerId!=0){
            List<MessageLog> messageLogs1 =messageLogDao.selectBySenderIdAndRecipientIdAndType((long)0,(long)0,(byte)6);
            List<MessageLog> messageLogs2 =messageLogDao.selectBySenderIdAndRecipientIdAndType((long)0,(long)0,(byte)1);
            messageLogs1.addAll(messageLogs2);
            for (MessageLog x1:messageLogs1){
                MessageListInfo messageListInfo = new MessageListInfo();
                messageListInfo.setId(x1.getId());
                messageListInfo.setSendTime(x1.getSendTime());
                messageListInfo.setContent(x1.getContent());
                messageListInfo.setType(x1.getType());
                messageListInfo.setIsRead(x1.getIsRead());
                messageListInfo.setSenderId(x1.getSenderId());
                messageListInfos.add(messageListInfo);
            }
        }
        responseDTO.success();
        responseDTO.setMsg("查询成功");
        responseDTO.setMessageListInfo(messageListInfos);
        return responseDTO;
    }

    @Override
    public BaseResponseDTO deleteMessageById(Integer id) {
        BaseResponseDTO responseDTO = new BaseResponseDTO();
        int num = messageLogDao.deleteByPrimaryKey(id);
        if (num<1){
            responseDTO.faild();
            responseDTO.setMsg("失败");
            return responseDTO;
        }
        responseDTO.success();
        responseDTO.setMsg("成功");
        return responseDTO;
    }

    @Override
    public GetFriendApplyListResponseDTO getFriendApplyList(Long playerId) {
        GetFriendApplyListResponseDTO responseDTO = new GetFriendApplyListResponseDTO();
        responseDTO.success();
        responseDTO.setMsg("查询成功");
        responseDTO.setFriendApplyListInfo(createFriendApplyListInfos(messageManager.getFriendApplyList(playerId)));
        return responseDTO;
    }

    @Override
    public BaseResponseDTO setIsRead(SetIsReadRequsetDTO requsetDTO) {
        BaseResponseDTO responseDTO = new BaseResponseDTO();
        MessageLog messageLog = messageLogDao.selectByPrimaryKey(requsetDTO.getId());
        if (messageLog==null){
            responseDTO.faild();
            responseDTO.setMsg("消息不存在");
            return responseDTO;
        }
        if (messageLog.getIsRead()==2){
            responseDTO.faild();
            responseDTO.setMsg("消息已经是已读");
            return responseDTO;
        }
        messageLog.setIsRead((byte)2);
        int num = messageLogDao.updateByPrimaryKeySelective(messageLog);
        if (num<1){
            responseDTO.faild();
            responseDTO.setMsg("失败");
            return responseDTO;
        }
        responseDTO.success();
        responseDTO.setMsg("成功");
        return responseDTO;
    }

    /**
     *将message 转换FriendApplyListInfo
     * */
    public FriendApplyListInfo createFriendApplyListInfo(MessageLog x) {
        FriendApplyListInfo friendApplyListInfo = new FriendApplyListInfo();
        friendApplyListInfo.setId(x.getId());
        friendApplyListInfo.setSendTime(x.getSendTime());
        friendApplyListInfo.setContent(x.getContent());
        friendApplyListInfo.setSenderId(x.getSenderId());
        friendApplyListInfo.setIsRead(x.getIsRead());
        friendApplyListInfo.setIsReceive(x.getIsReceive());

        PlayerInfo playerInfo = playerInfoManager.findPlayerInfoById(x.getSenderId());
        if(playerInfo != null){
            friendApplyListInfo.setNickName(playerInfo.getNickName());
            friendApplyListInfo.setHeadPic(playerInfo.getHeadPic());
        }
        return friendApplyListInfo;
    }

    public  List<FriendApplyListInfo>  createFriendApplyListInfos(List<MessageLog> messages) {
        List<FriendApplyListInfo> friendApplyListInfos = new ArrayList<FriendApplyListInfo>();
        for (MessageLog x:messages){
            friendApplyListInfos.add( createFriendApplyListInfo( x));
        }
        return friendApplyListInfos;
    }
}
